package com.yicong.ants.ui.circle.home;

import android.view.View;

import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.model.event.CommonEvent;
import com.cchao.simplelib.ui.fragment.SimpleLazyFragment;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.databinding.MyCircleFragmentBinding;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.ui.circle.ad.AdManagerActivity;
import com.yicong.ants.ui.circle.ad.AdPublishActivity2;
import com.yicong.ants.ui.circle.ad.AdViewedActivity;
import com.yicong.ants.ui.circle.article.ArticleTitleEditActivity;
import com.yicong.ants.ui.circle.article.MyArticleListActivity;

import java.util.Collections;


/**
 * 社区 tab
 *
 * @author cchao
 * @version 2019-07-31.
 */
public class MyCircleFragment extends SimpleLazyFragment<MyCircleFragmentBinding> implements View.OnClickListener {

    @Override
    protected int getLayoutId() {
        return R.layout.my_circle_fragment;
    }

    @Override
    protected void initEventAndData() {
        mDataBind.swipeRefresh.setOnRefreshListener(this::onLoadData);
    }

    @Override
    public void onFirstUserVisible() {
        if (UserManager.notLogin()) {
            showToast("请先登录账号");
        }
        mDataBind.setClick(this);
        onLoadData();
    }

    @Override
    protected void onLoadData() {
        if (UserManager.notLogin()) {
            showToast("请先登录账号");
            mDataBind.swipeRefresh.setRefreshing(false);
            return;
        }
        showProgress();
        addSubscribe(RetrofitHelper.getApis().summary(Collections.emptyMap())
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    return;
                }
                mDataBind.setBean(respBean.getData());
                mDataBind.swipeRefresh.setRefreshing(false);
            }, throwable -> {
                hideProgress();
                Logs.logException(throwable);
                mDataBind.swipeRefresh.setRefreshing(false);
            }));
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (event.getCode()) {
            case Constants.Event.Circle_Journal_Suc:
            case Constants.Event.Circle_Aritcle_Suc:
                onLoadData();
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_ad:
                Router.turnTo(mContext, AdManagerActivity.class).start();
                break;
            case R.id.my_viewed_ad:
                Router.turnTo(mContext, AdViewedActivity.class).start();
                break;
            case R.id.publish_ad:
            case R.id.publish_ad2:
                Router.turnTo(mContext, AdPublishActivity2.class).start();
                break;
            case R.id.edit_article:
                BusinessHelper.checkPublishTip(getActivity(), () -> {
                    Router.turnTo(mContext, ArticleTitleEditActivity.class).start();
                });
                break;
            case R.id.article_more:
                Router.turnTo(mContext, MyArticleListActivity.class).start();
                break;
        }
    }
}
